package com.neuedu.projectedu.controller;

import com.neuedu.projectedu.common.Result;
import com.neuedu.projectedu.entity.User;
import com.neuedu.projectedu.entity.request.RequestLogin;
import com.neuedu.projectedu.mapper.UserMapper;
import com.neuedu.projectedu.service.UserService;
import com.neuedu.projectedu.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Slf4j
@RequestMapping("/user")
public class UserController {
    @Value("${com.neuedu.secret}")
    private String secret;

    @Autowired
    private UserService userService;

    @GetMapping("/get/users")
    public Result<List<User>> getUserAll(){
        return Result.success(userService.getUsers());
    }

    @PostMapping("/add/user")
    public Result add(@RequestBody User user, HttpServletResponse response){
        if(userService.register(user)){
            return Result.success("添加成功");
        }else{
            return Result.fail();
        }
    }

    @PutMapping("/update/user")
    public Result update(@RequestBody User user){

        if(userService.update(user)!=null){
            return Result.success("修改成功");
        }else{
            return Result.fail("修改失败");
        }
    }

    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id") Long id){
        if(userService.removeById(id)){
            return Result.success("删除成功！");
        }else{
            return Result.fail("删除失败！");
        }
    }

    @PostMapping("/register")
    public String register(@RequestBody User user){
        // 代码 200 404 500 403 405 400
        //
        if(userService.register(user)){
            return "注册成功";
        }else{
            return "注册失败,该用户可能已存在！";
        }
    }

    @PostMapping("/login")
    public Result login(@RequestBody RequestLogin requestLogin){
//    public String login(String username,String password){
        String username=requestLogin.getUsername();
        String password=requestLogin.getPassword();
        int value = userService.login(username, password);
        String result="密码错误";
        switch (value) {
            case 0:
                log.info("该用户不存在");
                result="该用户不存在";
                break;
            case 1:
                log.info("用户存在，密码错误达3");
                result="用户存在，密码错误达3";
                break;
            case 2:
                log.info("时间没达1小时");
                result="时间没达1小时";
                break;
            case 3:
                log.info("成功");
                result=JwtUtils.sign(username,secret);
                Map<String,String> map=new HashMap<>();
                map.put("token",result);
                return Result.success(map);
            case 4:
                log.info("密码错误");
                result="密码错误";
                break;
        }
        return Result.fail(result);
    }
}
